﻿#region License
//  Distrubted Under the GNU Public License version 2 (GPLv2)
// ========================================
// 
//  QuizzMasterStudio Version 3 
//  Copyright © 2012 , The Sparks (Autism App Jam 2014)
//  All rights reserved.  
// 
// Robert Long, Zephyr Yao, Hillary Chansavang, Li Xiao, Arial Xiao
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//  The full license is also included in the root folder.
// 
// Robert Long
//  Email: rbtLong@live.com
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using Core.Annotations;
using Core.Features.Journal.ViewModel;
using Core.Persistence;
using Core.ScoreSystem;

namespace Core.User
{
    [DataContract]
    public class UserProfile : ISerializableItem, INotifyPropertyChanged
    {
        private string _userName = string.Empty;
        private string _phoneNumber = string.Empty;
        private List<JournalEntry> _journal = new List<JournalEntry>();
        private List<ScoreEntry> _score = new List<ScoreEntry>();

        public UserProfile()
        {
            BackgroundSelected = 0;
        }

        [DataMember]
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }

        [DataMember]
        public string PhoneNumber
        {
            get { return _phoneNumber; }
            set { _phoneNumber = value; }
        }
        
        [DataMember]
        public List<JournalEntry> Journal
        {
            get { return _journal; }
            set { _journal = value; }
        }

        [DataMember]
        public List<ScoreEntry> Score
        {
            get { return _score; }
            set { _score = value; }
        }

        [DataMember]
        public int BackgroundSelected { get; set; }

        public int TotalScore
        {
            get { return Score.Sum(o => o.Score); }
        }

        public void AppendScore(ScoreEntry score)
        {
            Score.Add(score);
            OnPropertyChanged("Score");
            OnPropertyChanged("TotalScore");
        }

        public event EventHandler BackgroundChanged = delegate { }; 
        public void ChangeBackground(int idx)
        {
            BackgroundSelected = idx;
            UserProfileSession.SaveSession();
            BackgroundChanged(this, new EventArgs());
        }

        public string PersistenceName { get { return FileName; } }

        public static string FileName { get { return "UserInfo.dat"; } }
        public static void Deserialize(out UserProfile profile)
        {
            profile = (new UserProfile()).Deserialize<UserProfile>();
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
